unit clsSequencia;

interface

uses Dm, dbtables, biblio, sysutils, clsBackup_Pedido, clspedidocompra;

type
  TSequencia = class(TObject)
  public
    function Ultimo_PedVenda: string;
    function Ultimo_PedCompra: string;
    procedure Anterior_PedCompra(pedcNum: string);
    function ExisteSequencia(p_Tipo, p_Num: string): Boolean;
  end;

implementation

function TSequencia.Ultimo_PedVenda;
var
  tq: TQuery;
  ultimo: string;
  backup: TBackupPedido;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Sequencia where Cod_Sequencia=:a');
  tq.params[0].asstring := '1';
  tq.open;
  if tq.eof then begin
    backup := TBackupPedido.Create;
    ultimo := strzero(backup.MaiorCodigo + 1, 9);
    backup.free;
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Insert into Sequencia values (:k_Cod_Sequencia,:k_Seq_PedVenda)');
  end else begin
    ultimo := strzero(tq.fieldbyname('Seq_PedVenda').asinteger + 1, 9);
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update Sequencia set Seq_PedVenda=:k_Seq_PedVenda where ' +
      'Cod_Sequencia=:k_Cod_Sequencia');
  end;
  tq.parambyname('k_Cod_Sequencia').asstring := '1';
  tq.parambyname('k_Seq_PedVenda').asstring := ultimo;
  tq.execsql;
  tq.close;
  tq.Free;
  result := ultimo;
end;

function TSequencia.Ultimo_Pedcompra;
var
  tq: TQuery;
  ultimo: string;
  ped: TPedidoComp;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Sequencia where Cod_Sequencia=:a');
  tq.params[0].asstring := '2';
  tq.open;
  if tq.eof then begin
    ped := TPedidoComp.Create;
    ultimo := ped.Ultimo;
    ped.free;
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Insert into Sequencia values (:k_Cod_Sequencia,:k_Seq_PedVenda)');
  end else begin
    ultimo := strzero(tq.fieldbyname('Seq_PedVenda').asinteger + 1, 9);
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update Sequencia set Seq_PedVenda=:k_Seq_PedVenda where ' +
      'Cod_Sequencia=:k_Cod_Sequencia');
  end;
  tq.parambyname('k_Cod_Sequencia').asstring := '2';
  tq.parambyname('k_Seq_PedVenda').asstring := ultimo;
  tq.execsql;
  tq.close;
  tq.Free;
  result := ultimo;
end;

procedure TSequencia.Anterior_PedCompra(pedcNum: string);
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update Sequencia set Seq_PedVenda=:k_Seq_PedVenda where ' +
    'Cod_Sequencia=:k_Cod_Sequencia');
  tq.parambyname('k_Cod_Sequencia').asstring := '2';
  tq.parambyname('k_Seq_PedVenda').asstring := StrZero(StrToInt(pedcNum) - 1, 9);
  tq.execsql;
  tq.close;
  tq.Free;
end;

function TSequencia.ExisteSequencia(p_Tipo, p_Num: string): Boolean;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Sequencia where Cod_Sequencia=:a and Seq_PedVenda=:b');
  tq.params[0].asstring := p_Tipo;
  tq.params[1].AsString := p_Num;
  tq.open;
  if not tq.Eof then
    Result := True
  else
    Result := False;
  tq.Close;
  tq.Free;
end;

end.

